import helper._

package jamun {
  /** Installed package info is got from rpm. By nature, it is a different model than the
      available package model
  */
  object InstalledPkgs {
    case class InstalledPkg(name:String, version:String, revision:String)
    
    lazy val list = queryRpms
    lazy val map = Map[String,InstalledPkg]() ++ (list map {p => p.name -> p})

    private def queryRpms = {
      // query all installed packages
      println ("Querying installed packages...")
      val rawList = execp ("rpm -q -a --qf %{N},%{V},%{R}\\n")
      assert(rawList._1 == 0)
      rawList._2 map (line => mkPkg(line.split(",")))
    }

    private def mkPkg(info:Array[String]) = {
      assert(info.length == 3)
      InstalledPkg(info(0), info(1), info(2))
    }

  } // InstalledPkgs

} // jamun
